package com.article.service.impl;

import com.article.dao.ArticleInfoDao;
import com.article.dao.ArticleTypeDao;
import com.article.dto.ReturnError;
import com.article.enums.ReturnInfoEnum;
import com.article.model.ArticleInfo;
import com.article.model.ArticleType;
import com.article.service.ArticleTypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/2.
 */
@Service("articleTypeService")
public class ArticleTypeServiceImpl implements ArticleTypeService {
    @Autowired
    private ArticleTypeDao articleTypeDao;

    public Long countTotal(Map<String, Object> map) {
        return articleTypeDao.countTotal(map);
    }

    public PageInfo<ArticleType> listByPage(Integer pageIndex, Integer limit, Map<String, Object> map) {
        PageHelper.startPage(pageIndex, limit);
        List<ArticleType> articleTypeList = articleTypeDao.listByPage(map);
        return new PageInfo<ArticleType>(articleTypeList);
    }

    public List<ArticleType> listByMap(Map<String, Object> map) {
        return articleTypeDao.listByMap(map);
    }

    public ArticleType getById(Long id) {
        return articleTypeDao.getById(id);
    }

    public ArticleType getByMap(Map<String, Object> map) {
        return articleTypeDao.getByMap(map);
    }

    @Transactional
    public ReturnError saveType(ArticleType articleType) {
        try {
            int result = articleTypeDao.save(articleType);
            if (result != 0) {
                return new ReturnError(false, ReturnInfoEnum.SUCCESS.getType(), ReturnInfoEnum.SUCCESS.getInfo(), null);
            }
            return new ReturnError(true, ReturnInfoEnum.FAIL.getType(), "操作失败", null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Transactional
    public ReturnError updateType(ArticleType articleType) {
        try {
            int result = articleTypeDao.update(articleType);
            if (result != 0) {
                return new ReturnError(false, ReturnInfoEnum.SUCCESS.getType(), ReturnInfoEnum.SUCCESS.getInfo(), null);
            }
            return new ReturnError(true, ReturnInfoEnum.FAIL.getType(), "操作失败", null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Transactional
    public ReturnError removeType(String ids) {
        try {
            String[] idarr = ids.split(",");
            for (String id : idarr) {
                int i = articleTypeDao.removeArt(Long.parseLong(id));
            }
            return new ReturnError(false, ReturnInfoEnum.SUCCESS.getType(), null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public List<ArticleType> listCount() {
        return articleTypeDao.listCount();
    }
}
